package my.proxy.rmi.server;

import my.state.State;
import my.state.headfirst.good.GunballMachine;
import my.proxy.rmi.GumballMachineRemote;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class GumballMachineProxy extends UnicastRemoteObject implements GumballMachineRemote {

    private GunballMachine gunballMachine;

    private String location;

    public GumballMachineProxy(String location,int numberGumballs) throws RemoteException {
        gunballMachine = new GunballMachine(numberGumballs);
        this.location = location;
    }

    public GumballMachineProxy rebind() throws Exception{
        Naming.bind("rmi://"+location+"/gumballMachine",this);
        return this;
    }

    public void insertQuarter() {
        gunballMachine.insertQuarter();
    }

    public void ejectQuarter() {
        gunballMachine.ejectQuarter();

    }

    public void turnCrank() {
        gunballMachine.turnCrank();
    }

    @Override
    public int getCount() throws RemoteException {
        return gunballMachine.getCount();
    }

    @Override
    public String getLocation() throws RemoteException {
        return location;
    }

    @Override
    public State getState() throws RemoteException {
        return (State) gunballMachine.getState();
    }
}
